<?php
/**
 * 获得URL
 * @return string
 */
function geturi(){
	$sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
	$php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
	$path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
	$relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self . (isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : $path_info);
	if(strpos($relate_url, '.html')){
		$relate_url = str_replace('.html', '', $relate_url);
	}
	$uri = $sys_protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $relate_url;
	return $uri;
}

/**
 * 字符串截取
 * @param $str
 * @param int $start
 * @param $length
 * @param bool|true $suffix
 * @param string $charset
 * @return string
 */
function msubstr($str, $start = 0, $length, $suffix = true, $charset = "utf-8"){
	$suffix = $suffix && strlen($str) > $length * 3 ? '...' : '';
	if(function_exists("mb_substr")){
		return mb_substr($str, $start, $length, $charset) . $suffix;
	}elseif(function_exists('iconv_substr')){
		return iconv_substr($str, $start, $length, $charset) . $suffix;
	}else{
		$re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
		$re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
		$re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
		$re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
		preg_match_all($re[$charset], $str, $match);
		$slice = join("", array_slice($match[0], $start, $length));
		return $slice . $suffix;
	}
}

/**
 * 日期格式化
 * @param $time
 * @return bool|string
 */
function formatTime($time){
	$today = strtotime(date('Y-m-d'));
	$yesterday = $today - 24 * 3600;
	$tomorrow = $today + 24 * 3600;
	if($time < $today && $time >= $yesterday){
		return '昨天 ' . date('H:i', $time);
	}elseif($time >= $today && $time < $tomorrow){
		return '今天 ' . date('H:i', $time);
	}elseif($time >= $tomorrow && $time < $tomorrow + 24 * 3600){
		return '明天 ' . date('H:i', $time);
	}else{
		return date('Y-m-d H:i', $time);
	}
}

/**
 * 获得随机字符串函数
 * @param int $length
 * @param int $type
 * @return string
 */
function getRandomString($length = 32, $type = 5){
	switch($type){
		case 1 :
			$chars = '0123456789';
			break;
		case 2 :
			$chars = 'abcdefghijklmnopqrstuvwxyz';
			break;
		case 3 :
			$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
			break;
		case 4 :
			$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
			break;
		case 6 :
			$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#$%^&*';
			break;
		default :
			$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	}
	$strlen = strlen($chars) - 1;
	for($i = 0; $i < $length; $i++){
		$str .= substr($chars, mt_rand(0, $strlen), 1);
	}
	return $str;
}

/**
 * 模拟Http
 * @param $url
 * @param $param
 * @param $data
 * @param string $method
 * @return mixed
 */
function http($url, $param, $data, $method = 'GET'){
	$url .= is_array($param) ? '?' . http_build_query($param) : '';
	$opts = array(CURLOPT_URL => $url, CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false);
	if($method == 'POST'){
		$opts[CURLOPT_POST] = true;
		$opts[CURLOPT_POSTFIELDS] = $data;
	}
	$ch = curl_init();
	curl_setopt_array($ch, $opts);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}
/**
 * 检查是否是以手机浏览器进入(IN_MOBILE)
 */
function isMobile() {
	$mobile = array();
	static $mobilebrowser_list ='Mobile|iPhone|Android|WAP|NetFront|JAVA|OperasMini|UCWEB|WindowssCE|Symbian|Series|webOS|SonyEricsson|Sony|BlackBerry|Cellphone|dopod|Nokia|samsung|PalmSource|Xphone|Xda|Smartphone|PIEPlus|MEIZU|MIDP|CLDC';
	//note 获取手机浏览器
	if(preg_match("/$mobilebrowser_list/i", $_SERVER['HTTP_USER_AGENT'], $mobile)) {
		return true;
	}else{
		if(preg_match('/(mozilla|chrome|safari|opera|m3gate|winwap|openwave)/i', $_SERVER['HTTP_USER_AGENT'])) {
			return false;
		}else{
			if($_GET['mobile'] === 'yes') {
				return true;
			}else{
				return false;
			}
		}
	}
}

/**
 * 去除HTML及空格、换行
 * [cutstr_html description]
 * @param  [type] $string [description]
 * @param  [type] $sublen [description]
 * @return [type]         [description]
 */
function cutstr_html($string,$sublen){
	$string = strip_tags($string);
	$string = trim($string);
	$string = mb_ereg_replace("\t","",$string);
	$string = mb_ereg_replace("\r\n","",$string);
	$string = mb_ereg_replace("\r","",$string);
	$string = mb_ereg_replace("\n","",$string);
	$string = mb_ereg_replace(" ","",$string);
	$string = mb_ereg_replace("　","",$string);
	$string = preg_replace('/^(&nbsp;|\s)*|(&nbsp;|\s)*$/', '', $string);

	return mb_substr($string,0,$sublen,'UTF-8')."...";

}
?>
